package com.wejoy.video.middle.station.data.util.baiduevs;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 调用百度EVS封装HttpClient常用方法
 * 
 * @author Wonder
 *
 */
public class EvsApiService {

    // 每个方法都会用到，提取出来
    private CloseableHttpClient httpClient;

    public EvsApiService() {
        this.httpClient = HttpClients.createDefault();
    }

    /**
     * 不带参数的get
     * 
     * @param uri
     * @return
     * @throws Exception
     */
    public HttpResult doGet(String uri) throws Exception {
        return this.doGet(uri,null, null);
    }

    /**
     * 带参数的get请求
     * 
     * @param url
     * @param map
     * @return
     * @throws Exception
     */
    public HttpResult doGet(String url,Map<String, String> headers, Map<String, String> map) throws Exception {
        // 1.创建URIBuilder
        URIBuilder uriBuilder = new URIBuilder(url);

        // 2.设置请求参数
        if (map != null) {
            // 遍历请求参数
            for (Map.Entry<String, String> entry : map.entrySet()) {
                // 封装请求参数
                uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
            }
        }

        // 3.创建请求对象httpGet
        HttpGet httpGet = new HttpGet(uriBuilder.build());
        //3.1 设置请求头
        if (!CollectionUtils.isEmpty(headers)) {
            for (String key : headers.keySet()) {
                httpGet.addHeader(key, headers.get(key));
            }
        }

        // 4.使用httpClient发起请求
        CloseableHttpResponse response = this.httpClient.execute(httpGet);

        // 5.解析返回结果，封装返回对象httpResult
        // 5.1获取状态码
        int code = response.getStatusLine().getStatusCode();

        // 5.2 获取响应体
        // 使用EntityUtils.toString方法必须保证entity不为空
        String body = null;
        if (response.getEntity() != null) {
            body = EntityUtils.toString(response.getEntity(), "UTF-8");
        }
        HttpResult result = new HttpResult();
        result.setCode(code);
        result.setBody(body);
        return result;
    }

    /**
     * 不带参数的post请求
     * 
     * @param url
     * @return
     */
    public HttpResult doPost(String url) throws Exception {
        return this.doPost(url, null,null);
    }

    /**
     * 带参数的post请求
     * 
     * @param url
     * @param map
     * @return
     * @throws Exception
     */
    public HttpResult doPost(String url, Map<String, String> headers, Map<String, String> map) throws Exception {
        // 1. 声明httppost
        HttpPost httpPost = new HttpPost(url);

        // 2.封装请求参数，请求数据是表单
        // 声明封装表单数据的容器
        List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
        if (map != null) {

            for (Map.Entry<String, String> entry : map.entrySet()) {
                // 封装请求参数到容器中
                parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }
        }
        // 创建表单的Entity类
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, "UTF-8");

        // 3. 把封装好的表单实体对象设置到HttpPost中
        httpPost.setEntity(entity);

        //3.1 设置请求头
        if (!CollectionUtils.isEmpty(headers)) {
            for (String key : headers.keySet()) {
                httpPost.addHeader(key, headers.get(key));
            }
        }
        // 4. 使用Httpclient发起请求
        CloseableHttpResponse response = this.httpClient.execute(httpPost);

        // 5. 解析返回数据，封装HttpResult
        // 5.1状态码
        int code = response.getStatusLine().getStatusCode();
        // 5.2 响应体内容
        String body = null;
        if (response.getEntity() != null) {
            body = EntityUtils.toString(response.getEntity(), "UTF-8");
        }

        HttpResult result = new HttpResult();
        result.setCode(code);
        result.setBody(body);
        return result;
    }

    /**
     * 不带参数的put请求
     * 
     * @param url
     * @return
     */
    public HttpResult doPut(String url) throws Exception {
        return this.doPut(url, null,null);
    }

    /**
     * 带参数的put请求
     * 
     * @param url
     * @param map
     * @return
     * @throws Exception
     */
    public HttpResult doPut(String url, Map<String, String> headers,Map<String, String> map) throws Exception {
        // 1. 声明httpput
        HttpPut httpPut = new HttpPut(url);
        if (!CollectionUtils.isEmpty(headers)) {
            for (String key : headers.keySet()) {
                httpPut.addHeader(key, headers.get(key));
            }
        }
        // 2.封装请求参数，请求数据是表单
        if (map != null) {
            // 声明封装表单数据的容器
            List<NameValuePair> parameters = new ArrayList<NameValuePair>();
            for (Map.Entry<String, String> entry : map.entrySet()) {
                // 封装请求参数到容器中
                parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }

            // 创建表单的Entity类
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, "UTF-8");

            // 3. 把封装好的表单实体对象设置到HttpPost中
            httpPut.setEntity(entity);
        }
        // 4. 使用Httpclient发起请求
        CloseableHttpResponse response = this.httpClient.execute(httpPut);

        // 5. 解析返回数据，封装HttpResult
        // 5.1状态码
        int code = response.getStatusLine().getStatusCode();
        // 5.2 响应体内容
        String body = null;
        if (response.getEntity() != null) {
            body = EntityUtils.toString(response.getEntity(), "UTF-8");
        }

        HttpResult result = new HttpResult();
        result.setCode(code);
        result.setBody(body);
        return result;
    }

    /**
     * 不带参数的delete
     * 
     * @param uri
     * @return
     * @throws Exception
     */
    public HttpResult doDelete(String uri) throws Exception {
        return this.doDelete(uri, null,null);
    }

    /**
     * 带参数的delete
     * 
     * @param url
     * @param map
     * @return
     * @throws Exception
     */
    public HttpResult doDelete(String url,Map<String, String> headers, Map<String, String> map) throws Exception {
        // 1.创建URIBuilder
        URIBuilder uriBuilder = new URIBuilder(url);
        // 2.设置请求参数
        if (map != null) {
            // 遍历请求参数
            for (Map.Entry<String, String> entry : map.entrySet()) {
                // 封装请求参数
                uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
            }
        }

        // 3.创建请求对象httpGet
        HttpDelete httpDelete = new HttpDelete(uriBuilder.build());
        if (!CollectionUtils.isEmpty(headers)) {
            for (String key : headers.keySet()) {
                httpDelete.addHeader(key, headers.get(key));
            }
        }
        // 4.使用httpClient发起请求
        CloseableHttpResponse response = this.httpClient.execute(httpDelete);

        // 5.解析返回结果，封装返回对象httpResult
        // 5.1获取状态码
        int code = response.getStatusLine().getStatusCode();

        // 5.2 获取响应体
        // 使用EntityUtils.toString方法必须保证entity不为空
        String body = null;
        if (response.getEntity() != null) {
            body = EntityUtils.toString(response.getEntity(), "UTF-8");
        }
        HttpResult result = new HttpResult();
        result.setCode(code);
        result.setBody(body);
        return result;
    }

}
